import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-01-07
 * Time: 14:58
 */
public class test1 {
    /*
    LeetCode 169 多数元素
     */
    public int majorityElement(int[] nums) {
        Map<Integer,Integer> counts = countNums(nums);
        Map.Entry<Integer,Integer> majorEntry=null;
        for(Map.Entry<Integer,Integer> entry:counts.entrySet()){
            if (majorEntry == null || entry.getValue() > majorEntry.getValue()){
                majorEntry = entry;
            }
        }
        return majorEntry.getKey();
    }

    private Map<Integer,Integer> countNums(int[] nums){
        Map<Integer,Integer> counts = new HashMap<Integer,Integer>();
        for(int num:nums){
            // 没有这个值就加入进去然后加一，有就该值多加一个
            if(!counts.containsKey(num)){
                counts.put(num,1);
            }else{
                counts.put(num,counts.get(num)+1);
            }
        }
        return counts;
    }
}
